******************************************************************************************
* Do-file name:	an_reg_occupg_task.do                                                    
* Task:         run regressions on share task employment, municipality level
* Last change:  28.07.2025                                                               
* Notes:                                                                                 
/*
This file contains the code used to generate the results presented in the following tables and figures:
- Figure 2, Panel B: 
		-- Abstract tasks: ds_emp_nat_task3d22
*/
******************************************************************************************



******************************************************************************************
*** program setup
******************************************************************************************

version 14.2
clear all
macro drop _all
set linesize 90
set more off
* set trace on
discard
set seed 123456789
*set matsize 2000



******************************************************************************************
*** load working dataset
******************************************************************************************

use "data/employ_region_s1.dta", clear


*** create outcome variables using 2- and 3-digit occupational tasks
foreach d of numlist 3 {

** change share of native abstract and non-abstract workers between year X and 1990: 
foreach x in 21 22 {
gen ds_emp_nat_task`d'd`x' = (emp_nat_task`d'd`x' / emp_nat_task`d'd) - (emp_nat_task`d'd`x'_90 / emp_nat_task`d'd_90)
replace ds_emp_nat_task`d'd`x' = 0  if year == 1990
	}


******************************************************************************************
*** set globals
******************************************************************************************

* 1) 
global group_g_emp_nat_task`d'd21 "Manual/routine tasks worker (natives)"
global weight_emp_nat_task`d'd21  "emp_nat_task`d'd_90*weight_matching"

* 2) 
global group_g_emp_nat_task`d'd22 "Abstract tasks worker (natives)"
global weight_emp_nat_task`d'd22  "emp_nat_task`d'd_90*weight_matching"


*** use different czech shocks
foreach year of numlist 1985/1990 1992/1995 {
global czech_shock_`year' "czshare_92_90_predic_13"
	}

global czech_shock_1991   "czshare_91_90_predic_13"


******************************************************************************************
*** estimate employment change
******************************************************************************************

*** create global for loop
global emp_nat_string "emp_nat_task`d'd22"


*** run different samples 				
foreach var of global emp_nat_string {

foreach y of numlist 1987/1995 {

*** 1) basic model: full sample
reg ds_`var'  ${czech_shock_`y'}  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}], cluster(ao_kreis_imp)
est store `var'`y'


*** check sample
dis "Outcome Variable: ${group_g_`var'}"
dis "Year = `y'"
dis "Sample restriction version: 16-65, employed natives (pers_gr: 101 & 102)"
count if e(sample)


********* bootstrap standard errors ***************************************************************
preserve
keep if e(sample)
drop czshare_92_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_92_`bs'.dta", nogenerate
	reg ds_`var'  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	scalar coef`bs' = _b[czshare_92_90_predic_13]
	drop y res xb czshare_92_90_predic_13 wild
}
gen bs = . 
gen coef_g_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_g_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_g_`var'_`y'
keep if bs != .
save "data\bootstrap\occ_upg/bs_coef_ds_`var'_`y'.dta", replace
sum 			// std. dev. = std. error
restore

***************************************************************************************************

********* bootstrap standard errors 1991 **********************************************************
if `y' == 1991 {
preserve
keep if e(sample)
drop czshare_91_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_91_`bs'.dta", nogenerate
	reg ds_`var'  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_`var'}]
	scalar coef`bs' = _b[czshare_91_90_predic_13]
	drop y res xb czshare_91_90_predic_13 wild
}
gen bs = . 
gen coef_g_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_g_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_g_`var'_`y'
keep if bs != .
save "data\bootstrap\occ_upg\1991_cz_shock/bs_coef_ds_`var'_`y'.dta", replace
sum 			// std. dev. = std. error
restore
}
***************************************************************************************************
	}


*** create tables
#delimit ;
global estout_employ "cells(b(star fmt(%9.3f) vacant(-)) se(fmt(%9.3f) par)) 
stats(N N_clust r2_a, labels("Observations" "Clusters" "Adjusted R2") layout(@ @ @) fmt(%9.0fc %9.0fc %9.3f))
starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30)
varlabels(_cons "Constant" czshare_92_90_predic_13 "Share Czechs (92-90, pred.)") label
mlabels("OccuUp. 87-90" "OccuUp. 88-90" "OccuUp. 89-90" "OccuUp. 91-90" "OccuUp. 92-90" "OccuUp. 93-90" "OccuUp. 94-90" "OccuUp. 95-90")
prehead(@title) posthead() postfoot(@note) nonumbers collabels(none) style(tab)";
#delimit cr


** Table: Employment
estout  `var'1987  `var'1988  `var'1989  `var'1991  `var'1992  `var'1993  `var'1994  `var'1995 ///
using "tables\employ/tab_ds_`var'.txt", $estout_employ replace ///	
title(Change Native Occupations (Tasks) 1987-1995: Base, Sample: ${group_g_`var'}) ///
note(Notes: Regional approach. Occupational upgrading measured using tasks. * p<0.1, ** p<0.05, *** p<0.01. ///
Data Source: German Social Security Records, border districts and matched control districts, 1987-1995.)
	}
	}


******************************************************************************************
*** end
******************************************************************************************

exit


*========================================================================================*
Comments:
- unique identifier: vsnr_ano year
